package com.xzx.framework.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


/**
 * 时间工具类
* @ClassName: DateUtil    
* @Description:     
* @author xzx   
* @date 2018年3月7日 下午5:14:18    
*
 */
public abstract class DateUtil {
	/**
	 * yyyy-MM
	 */
	public static final String PATTERN_YM = "yyyy-MM";
	/**
	 * yyyy-MM-dd
	 */
	public final static String PATTERN_DATE1 = "yyyy-MM-dd";
	/**
	 * yyyy/MM/dd
	 */
	public final static String PATTERN_DATE2 = "yyyy/MM/dd";
	/**
	 * yyyy年MM月dd日
	 */
	public final static String PATTERN_DATE3 = "yyyy年MM月dd日";
	/**
	 * HH:mm
	 */
	public final static String PATTERN_TIME1 = "HH:mm";
	/**
	 * HH:mm:ss
	 */
	public final static String PATTERN_TIME2 = "HH:mm:ss";
	/**
	 * yyyy-MM-dd'T'HH:mm:ss
	 */
	public final static String PATTERN_DATE_TIME1 = "yyyy-MM-dd'T'HH:mm:ss";
	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public final static String PATTERN_DATE_TIME2 = "yyyy-MM-dd HH:mm:ss";
	/**
	 * yyyyMMddHHmmss
	 */
	public static final String PATTERN_DATE_TIME3 = "yyyyMMddHHmmss";
	/**
	 * yyyyMMddHHmm
	 */
	public static final String PATTERN_DATE_TIME4 = "yyyyMMddHHmm";
	/**
	 * yyyyMMddHH
	 */
	public static final String PATTERN_DATE_TIME5 = "yyyyMMddHH";
	/**
	 * yyyyMMddHHmmssSSS
	 */
	public static final String PATTERN_DATE_TIME6 = "yyyyMMddHHmmssSSS";
	/**
	 * yyyy-MM-dd HH:mm
	 */
	public static final String PATTERN_DATE_TIME7 = "yyyy-MM-dd HH:mm";
	/**
	 * yyyyMMdd
	 */
	public static final String PATTERN_DATE_TIME8 = "yyyyMMdd";
	/**
	 * yyyy年MM月dd日 HH
	 */
	public final static String PATTERN_DATE_TIME9 = "yyyy年MM月dd日 HH时mm分ss秒";
	

	/**
	 * 时间单位。年
	 */
	public final static int UNIT_YEAR = 1;
	/**
	 * 时间单位。月
	 */
	public final static int UNIT_MONTH = 2;
	/**
	 * 时间单位。周
	 */
	public final static int UNIT_WEEK = 3;
	/**
	 * 时间单位。天
	 */
	public final static int UNIT_DAY = 4;
	/**
	 * 时间单位。时
	 */
	public final static int UNIT_HOUR = 5;
	/**
	 * 时间单位。分
	 */
	public final static int UNIT_MINUTE = 6;
	/**
	 * 时间单位。秒
	 */
	public final static int UNIT_SECOND = 7;

	
	/**
	 * 默认的日期格式
	 */
	private final static SimpleDateFormat defaulFormat = new SimpleDateFormat(PATTERN_DATE1);
	
	public static SimpleDateFormat yyyyMMddHHmmss;
	

	/**
	 * 获取当年年份
	 * @return
	 */
	public static int getCurYear() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}
	
	/**
	 * 按默认格式(yyyy-MM-dd)格式化日期
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		return defaulFormat.format(date);
	}
	
	/**
	 * 按指定格式格式化日期
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatDate(Date date, String pattern){
		return new SimpleDateFormat(pattern).format(date);
	}
	
	/**
	 * 按指定格式解析日期
	 * @param dateStr
	 * @param pattern
	 * @return
	 */
	public static Date parseDate(Object dateStr, String pattern){
		DateFormat df = new SimpleDateFormat(pattern);
		try {
			if(CommonUtil.isNull(dateStr)) {
				return new Date();
			}
			return df.parse(dateStr.toString());
		} catch (ParseException e) {
			throw new RuntimeException(e.getLocalizedMessage());
		}
	}
	
	/**
	 * 获取指定日期在指定时间长度之后的日期。
	 * @param date
	 * @param offset
	 * @param unit
	 * @return
	 */
	public static Date getDateOffset(Date date, int offset, int unit) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        if (offset != 0) {
			switch (unit) {
				case UNIT_YEAR:
					cal.add(Calendar.YEAR, offset);
					break;
				case UNIT_MONTH:
					cal.add(Calendar.MONTH, offset);
					break;
				case UNIT_WEEK:
					cal.add(Calendar.WEEK_OF_YEAR, offset);
					break;
				case UNIT_DAY:
					cal.add(Calendar.DAY_OF_YEAR, offset);
					break;
				case UNIT_HOUR:
					cal.add(Calendar.HOUR_OF_DAY, offset);
					break;
				case UNIT_MINUTE:
					cal.add(Calendar.MINUTE, offset);
					break;
				case UNIT_SECOND:
					cal.add(Calendar.SECOND, offset);
					break;
			}
        }
        return cal.getTime();
    }
	
	/**
	 * 获取“年”
	 * @param date
	 * @return
	 */
	public static int getYear(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}

	
	/**
	 * 获取“月”
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取“日”
	 * @param date
	 * @return
	 */
	public static int getDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 获取“小时”
	 * @param date
	 * @return
	 */
	public static int getHour(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.HOUR_OF_DAY);
	}
	
	/**
	 * 获取“分钟”
	 * @param date
	 * @return
	 */
	public static int getMinute(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MINUTE);
	}
	
	/**
	 * 获取“秒”
	 * @param date
	 * @return
	 */
	public static int getSecond(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.SECOND);
	}
	
	/**
	 * 取得指定日期的第二天
	 * @param date
	 * @return
	 */
	public static Date getNextDay(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	
	/**
	 * 取得指定日期的00:00:00.000
	 * @param date
	 * @return
	 */
	public static Date getStartOfDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 取得指定日期的最后一秒
	 * @param date
	 * @return
	 */
	public static Date getLastSecOfDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}
	
	/**
	 * 判断两个日期是否是同一天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Date date1, Date date2){
		int y1 = getYear(date1);
		int m1 = getMonth(date1);
		int d1 = getDay(date1);
		
		int y2 = getYear(date2);
		int m2 = getMonth(date2);
		int d2 = getDay(date2);
		
		return (y1 == y2) && (m1 == m2) && (d1 == d2);
	}
	
	/**
	 * 验证日期是否合法
	 * @param year
	 * @param month
	 * @param day
	 */
	public static void validate(int year, int month, int day) {
		if(year < 1) {
			throw new IllegalArgumentException("年份不能小于1");
		}
		if(month < 1 || month > 12) {
			throw new IllegalArgumentException("月份只能取值1~12");
		}
		
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		c.set(Calendar.DAY_OF_MONTH, 1);
		
		c.add(Calendar.DAY_OF_MONTH, -1);
		
		int lastDay = c.get(Calendar.DAY_OF_MONTH);
		if(day < 1 || day > lastDay) {
			throw new IllegalArgumentException("日取值不能超过" + 1 + "~" + lastDay);
		}
	}
	
	/**
	 * 计算两个日期相差的天数
	 * 若date1比date2小，返回正数；
	 * 若date1比date2大，返回负数；
	 * 若相等，返回0
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getDayDiff(Date date1, Date date2){
		long dif = (date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24);
		return (int) dif;
	}
	
	public static void main(String[] args) {
		String dateTime = "2017-08-23 17:10:00";
		Date nowDate = parseDate(dateTime, PATTERN_DATE_TIME2);
		System.out.println(getDayDiff(nowDate, new Date()));
	}
	
	   /**
     * 把日期转换为字符串
     *
     * @param date 被转换的日期
     * @return 转换后的字符串，格式为yyyy-MM-dd HH:mm:ss
     */
    public static String timestampTrferStr(Date date) {
        if (date == null)
            return null;
        String str = null;
        yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            str = yyyyMMddHHmmss.format(date);
        } catch (Exception e) {
            return null;
        }
        return str;
    }
    
    public static String timestampTrferStr(Date date, String dateType) {
        if (date == null)
            return null;
        String str = null;
        yyyyMMddHHmmss = new SimpleDateFormat(dateType);
        try {
            str = yyyyMMddHHmmss.format(date);
        } catch (Exception e) {
            return null;
        }
        return str;
    }
    
    
	/**
	 * 得到现在的时间，格式时：分：秒
	 * 
	 * @param format
	 *            格式,如HH:mm:ss
	 * @return 返回现在的时间可用于插入数据库和显示
	 */
	public static Date getNowTime(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date= sdf.parse(sdf.format(new java.util.Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 获取当月第一天
	 * @param datadate
	 * @return
	 * @throws Exception
	 */
	public static Date getFirstDay(String datadate){
		Date date = parseDate(datadate,PATTERN_DATE1);
		
		//创建日历
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	
	/**
	 * 获取当月最后一天
	 * @param datadate
	 * @return
	 * @throws Exception
	 */
	public static Date getLastDay(String datadate){
		Date date = parseDate(datadate,PATTERN_DATE1);
		
		//创建日历
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);    //加一个月
		calendar.set(Calendar.DATE, 1);     //设置为该月第一天
		calendar.add(Calendar.DATE, -1);    //再减一天即为上个月最后一天
		return calendar.getTime();
	}
	
	/**
	 *格式化放映时间 yyyy-MM-dd hh:mm - hh:mm 
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static String formateShowTime(Date startTime, Date endTime){
		String start = formatDate(startTime, PATTERN_DATE_TIME2);
		String end = formatDate(endTime, PATTERN_DATE_TIME2);
//		start.substring(0, 16)+end.substring(15);
		return start.substring(0, 16)+"-"+end.substring(11,16);
		
	}
}
